﻿#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
from DB.DB_Classes import *


def __get_complete_facs_json(sem):
    file_path = os.path.join(os.path.split(__file__)[0], "Data_Dumps\\" + sem + ".txt")
    f = open(file_path, "r")
    ret_json = json.load(f, 'utf-8')
    f.close()
    return ret_json


def importer(sem=u"201401"):
    complete_facs_json = __get_complete_facs_json(sem)
    courses_list = []
    fac_list = []
    for fac in complete_facs_json:
        fac_list.append((fac["fac_name"], fac["fac_num"]))
        curr_fac = FacultyForSem(id=sem + "_" + fac["fac_num"], semester=sem, fac_name=fac["fac_name"],
                                 fac_num=fac["fac_num"], courses=fac["fac_courses"])
        curr_fac.put()

        for course in fac["fac_courses"].values():
            courses_list.append({"courseCode": course["course_num"],
                                 "courseName": course["course_name"] + " (" + course["course_num"] + ")"})

    courses_list.sort(key=lambda x: x['courseName'])
    SemesterData(id=sem, semester=sem, courses_list=courses_list, faculties=fac_list).put()


if __name__ == '__main__':
    importer()